﻿/**
as3app - An open-source ActionScript 3.0 Library developed to assist 
in creating simple visual controls/data binding/asset loading for the
Flash player platform.

http://code.google.com/p/as3app/

Copyright (c) 2011 M. K. Chowdhury, All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package as3app.tooltip 
{
	import flash.display.DisplayObject;
	import flash.display.Shader;
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.display.GradientType;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextLineMetrics;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author Muntasir Kabir Chowdhury
	 */
	public class TypeOfDynamicTooltip extends Sprite implements ITooltip
	{
		protected var _fraction:Number = 0;
		protected var _timer:Timer = null;
		
		protected var _refreshDelay:int = 0;
		protected var _callbackTimer:Timer = null;
		protected var _callback:Function = null;
		protected var _arrayOfTexts:Array = null;
		
		public function TypeOfDynamicTooltip(callback:Function,refreshDelay:int) 
		{
			_callback = callback;
			_refreshDelay = refreshDelay;
			_callbackTimer = new Timer(refreshDelay, 0);
			_callbackTimer.addEventListener(TimerEvent.TIMER, On_CallbackTimerTick);
			
			mouseEnabled = false;
			mouseChildren = false;
		}
		
		
		public function StartToolTip():DisplayObject
		{
			if (Tooltip.TooltipDelay > 0)
			{
				_timer = new Timer(Tooltip.TooltipDelay, 1);
				_timer.addEventListener(TimerEvent.TIMER_COMPLETE, On_DelayComplete);
				_timer.start();
				
				alpha = 0;
			}
			else if (Tooltip.TooltipSoftOnTime > 0) 
			{
				_timer = new Timer(50, 1);
				On_DelayComplete(null);
			}
			else
			{
				_callbackTimer.start();
				alpha = 1;
			}
			
			return this;
		}
		
		
		protected function On_DelayComplete(e:Event)
		{
			_timer.reset();
			_callbackTimer.start();
			if (Tooltip.TooltipSoftOnTime > 0)
			{
				_timer.repeatCount = Math.ceil(Tooltip.TooltipSoftOnTime / 50);
				_timer.delay = 50;
				_fraction = (1 - Tooltip.TooltipSoftOn) / _timer.repeatCount;
				
				if (_timer.hasEventListener(TimerEvent.TIMER_COMPLETE)) _timer.removeEventListener(TimerEvent.TIMER_COMPLETE, On_DelayComplete);
				_timer.addEventListener(TimerEvent.TIMER_COMPLETE, On_SoftOnComplete);
				_timer.addEventListener(TimerEvent.TIMER, On_TimerTickWhileSoftOn);
				this.alpha = Tooltip.TooltipSoftOn;
				_timer.start();
				//trace("alpha:" + alpha + "  _fraction:" + _fraction);
			}
		}
		
		protected function On_TimerTickWhileSoftOn(e:Event)
		{
			this.alpha += _fraction;
			//trace("fraction :"+_fraction+"  alpgha:"+ alpha);
		
		}
		
		protected function On_SoftOnComplete(e:Event)
		{
			_timer = null;
			this.alpha = 1;
		}
		
		public function StopTooltip():DisplayObject
		{
			_timer = null;
			_callbackTimer.stop();
			
			return this;
		}
		
		public function SetCallback(callback:Function):void
		{
			
		}
		
		public function GetToolTip():DisplayObject
		{
			return this as DisplayObject;
		}
		
		
		protected function On_CallbackTimerTick(e:Event)
		{
			_arrayOfTexts = _callback.call(null) as Array;
			ReDraw();
		}
		
		public function ReDraw():void
		{
			// Clear
			while (numChildren > 0) removeChildAt(0);
			
			
			// Draw
			var lastY:int = Tooltip.BorderWidth;
			var paraWidth:int = Tooltip.Width - Tooltip.BorderWidth - Tooltip.BorderWidth;
			var sprite:Sprite = new Sprite();
			
			for (var i:int = 0; i < _arrayOfTexts.length;++i )
			{	
				var text:TextField = new TextField();
				
				text.width = paraWidth;
				text.multiline = true;
				text.wordWrap = true;

				text.text = String(_arrayOfTexts[i]);
				var metrics:TextLineMetrics = text.getLineMetrics(0);
				text.height =   text.textHeight + metrics.ascent;
				//trace("@@@@@ 2" + text.height + "  lineHeight=" + text.textHeight );// + "  Added=" + String(text.textHeight + toBeAdded));
				
				var shape1:Shape = new Shape();
				shape1.graphics.beginGradientFill(GradientType.RADIAL, [0x301070, 0x301099], [.9, .9],[1,50]);
				shape1.graphics.lineStyle(1, 0x113366, .5);
				shape1.graphics.drawRect(0, 0, paraWidth, text.height);
				shape1.graphics.endFill();
				shape1.y = lastY;
				shape1.x = Tooltip.BorderWidth;
				addChildAt(shape1, 0);
				
				text.y = lastY;
				text.x = Tooltip.BorderWidth;
				addChild(text);
				
				lastY += text.textHeight + 15;
			}
			
			var shape:Shape = new Shape();
			shape.graphics.beginGradientFill(GradientType.RADIAL, [0x301070, 0x301099], [.7, .7],[1,50]);
			shape.graphics.lineStyle(0, 0x113366, 1);
			shape.graphics.drawRoundRect(0, 0, this.width + Tooltip.BorderWidth + Tooltip.BorderWidth, this.height + Tooltip.BorderWidth + Tooltip.BorderWidth,Tooltip.CornerEllipseWidth,5);
			shape.graphics.endFill();
			addChildAt(shape, 0);
			
			// Clear
			//while (numChildren > 0) removeChildAt(0);
			//addChild(sprite);
			
			mouseEnabled = false;
			mouseChildren = false;
		}
	}

}